<template>
  <div>
    <el-card class="box-card">
      <!-- 面包屑导航 -->
      <el-breadcrumb separator-class="el-icon-arrow-right">
        <el-breadcrumb-item :to="{ path: '/welcome' }">首页</el-breadcrumb-item>
        <el-breadcrumb-item>商品管理</el-breadcrumb-item>
        <el-breadcrumb-item>商品列表</el-breadcrumb-item>
      </el-breadcrumb>
      <!-- 商品搜索框和按钮 -->
      <el-row :gutter="20">
        <el-col :span="6">
          <el-input placeholder="请输入内容" autofocus class="input-with-select" clearable
                    ref="searchInput" v-model="queryInfo.search" :disabled="searchProhibit"
                    @keyup.native.enter="handleSearchEnter" @clear="handleSearchEnter">
            <el-button :disabled="searchBtnProhibit" slot="append" icon="el-icon-search"
                       @click="handleSearchWithProhibit(prohibitTimeout,'searchBtnProhibit')"></el-button>
          </el-input>
        </el-col>
        <el-col :span="2">
          <el-button type="primary">添加商品</el-button>
        </el-col>
      </el-row>
      <el-table
        v-loading="loading"
        element-loading-text="拼命加载中"
        element-loading-spinner="el-icon-loading"
        element-loading-background="rgba(255, 255, 255, 0.7)"
        border stripe width="100%"
        :data="goodsList">
        <el-table-column type="index" label="#"></el-table-column>
        <el-table-column prop="goods_name" label="商品名称"></el-table-column>
        <el-table-column prop="goods_price" label="商品价格（元）" width="110px"></el-table-column>
        <el-table-column prop="goods_weight" label="商品重量" width="100px"></el-table-column>
        <el-table-column label="创建时间" width="150px">
          <template v-slot="scope">{{ scope.row.add_time | dateFormat }}</template>
        </el-table-column>
        <el-table-column label="操作" width="120px">
          <template v-slot="scope">
            <el-button type="primary" icon="el-icon-edit"></el-button>
            <el-button type="danger" icon="el-icon-delete"
                       @click="deleteGoods(scope.row.goods_id,scope.$index)"></el-button>
          </template>
        </el-table-column>
      </el-table>
      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="queryInfo.pagenum"
        :page-size="queryInfo.pagesize"
        :total="total"
        :page-sizes="[1, 5, 10, 20, 50]"
        layout="total, sizes, prev, pager, next, jumper">
      </el-pagination>
    </el-card>
  </div>
</template>

<script>
import common from '@/assets/js/common'

export default {
  data () {
    return {
      // 节流阀时长
      prohibitTimeout: 200,
      // 搜索框禁用
      searchProhibit: false,
      // 搜索按钮禁用
      searchBtnProhibit: false,
      loading: false,
      // 数据列表查询参数
      queryInfo: {
        search: '',
        pagenum: 1,
        pagesize: 10
      },
      // 商品列表数据
      goodsList: [],
      // 总数量
      total: 0
    }
  },
  created () {
    // 获取商品列表数据
    this.getGoodsList()
  },
  methods: {
    // 获取商品列表数据
    async getGoodsList () {
      this.loading = true
      const { data: res } = await this.http.get('goods', { params: this.queryInfo })
      if (this.failed(res, 200)) return
      this.goodsList = res.data.goods
      this.total = res.data.total
      this.loading = false
    },
    // 处理页面大小改变
    handleSizeChange (pagesize) {
      this.queryInfo.pagesize = pagesize
      this.getGoodsList()
    },
    // 处理当前页码改变
    handleCurrentChange (pagenum) {
      this.queryInfo.pagenum = pagenum
      this.getGoodsList()
    },
    // 刷新商品列表并禁用元素一定时间
    // 参数1：禁用时长，参数2：元素数组
    async handleSearchWithProhibit (duration, ...props) {
      await common.preventDuplicateSubmit(prohibit => {
        if (!props) return
        props.forEach(item => {
          this[item] = prohibit
        })
      }, async () => {
        await this.getGoodsList()
      }, duration)
    },
    // 搜索框回车事件
    async handleSearchEnter () {
      await common.preventDuplicateSubmit(prohibit => {
        this.searchProhibit = prohibit
        // 当输入框启用30ms后，获取焦点
        if (!prohibit) {
          window.setTimeout(() => {
            this.$refs.searchInput.focus()
          }, 30)
        }
      }, async () => {
        await this.getGoodsList()
      })
    },
    async deleteGoods (goodsId, index) {
      const confirmRes = await this.$confirm('此操作将永久删除该文件, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).catch(err => err)
      if (confirmRes === 'cancel') return
      const { data: res } = await this.http.delete(`goods/${goodsId}`)
      if (this.failed(res, 200, true)) return
      this.goodsList.splice(index, 1)
    }
  }
}
</script>

<style lang="less" scoped>

</style>
